<?php
namespace Modules\Admin\Model\Service\Access;

/**
 * 管理系统权限控制相关配置
 */
class Config {
    
    const   ACL_UID = 'uid';
    const   ACL_GID = 'gid';
    const   ACL_ALL = 'all';
    const   ACL_UGROUP = 'usergroup';

    public static $action_pattern = '/Action$/';
    public static $action_suffix = 'Action';
    public static $class_pattern = '/^Controller_/i';
    public static $class_prefix = 'Controller_';
    public static $use_autoload = false;
    public static $no_login_controller = array('Login','Sso');
    #@deprecated
    private static $allow_ip = array(
        '106.38.223.5', '106.38.223.163', '116.90.80.160',
        '124.205.127.66', '124.205.127.134', '223.223.192.210', '116.228.56.170',
        '103.249.244.111', '180.166.153.234',
        '218.245.8.111','1.119.184.210',
    );

    /**
     * 获取管理后台允许访问的IP地址
     * @return array
     */
    public static function getAllowIp() {
        $admin_ips_file = getenv("ADMIN_IPS_FILE")?:"/etc/config/admin_ips.conf";
        $admin_ips = self::$allow_ip;
        if (file_exists($admin_ips_file)) {
            $arr = file($admin_ips_file, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
            foreach($arr as $key=>$line) {
                $line = trim(preg_replace("/#.*$/", "", $line));
                if (empty($line)) {
                    unset($arr[$key]);
                    continue;
                }
                $arr[$key] = $line;
            }
            $admin_ips = array_unique($arr);
        }
        return $admin_ips;
    }
    /**
     * 获取 action 的规则
     * @return array
     */
    public static function getActionRules() {
        return array(
            'classes' => self::$class_pattern,
            'class_prefix' => self::$class_prefix,
            'methods' => self::$action_pattern,
            'use_autoload' => self::$use_autoload,
        );
    }

    /**
     * 添加不许要登录也可以访问的控制器
     * @param array $controller
     * @return bool
     */
    public static function setNoLoginController(array $controller) {
        self::$no_login_controller = array_merge(self::$no_login_controller, $controller);
    }
}
